<?php
namespace Home\Controller;
use Think\Controller;
use Home\Controller\BaseController;
class SupplierController extends BaseController
{
    public function _initialize(){
         parent::_initialize();
        $where['store_id'] = I("suppId") ? I("suppId"):I("get.store_id");
        $this->assign('store_id',$where['store_id']);
        $store = M('Store')->where($where)->find();
        $this->assign('store',$store);
        //        $store_id = I('get.store_id');
        $store_slide = unserialize($store['store_slide']);//幻灯片
        $store_slide_url = unserialize($store['store_slide_url']);//链接地址

        foreach ($store_slide as $key => $value) {
            if(empty($value)){
                unset($store_slide[$key]);
            }
        }
       
        $this->assign('store_slide',$store_slide);
        $this->assign('store_slide_url',$store_slide_url);
        $store_goods_class = M('Store_goods_class');
        $goods_class = $store_goods_class->where($where)->order('stc_sort desc')->select();

        //店铺导航栏
        $store_navigation = M('store_navigation')->where(array('sn_store_id'=>$where['store_id']))->select();
        $this->assign('store_navigation',$store_navigation);

        //店铺关注数量
        $fa['fav_id'] = I("suppId");
        $fa['fav_type'] = 40;
        $favoritesNum = M('Favorites')->where($fa)->count();
        $favoritesNum ? true : $favoritesNum = 0;
        $this->assign('favoritesNum',$favoritesNum);

        //登陆判断是否收藏
        if(session('user_id')){
            $fa['member_id'] = session('user_id');
            $user_num = M('Favorites')->where($fa)->count();
            $this->assign('user_num',$user_num);
        }

        //店铺 服务 物流 评价得分
        

        $this->assign('goods_class',$goods_class);
        $this->assign('supplier','1');
    }

    //店铺首页
    public function index(){
        $goods = M('goods');
        $where['store_id'] = I("suppId");
        $where['goods_state'] = 1;
        $where['goods_sale'] = 1;
        
        $map['store_id'] = I("suppId");

        //今日新品
        $goods_new = $goods->where($where)->limit(6)->order('goods_created desc')->select();
        $this->assign('goods_new',$goods_new);
        //销量排行榜
        $goods_sales = $goods->where($where)->limit(10)->order('goods_realsale desc')->select();
        $this->assign('goods_sales',$goods_sales);

        //分类展示商品
        $store_goods_class = M("store_goods_class")->where($map)->select();
        if($store_goods_class){
            foreach($store_goods_class as $k=>$v){
                $maps['stc_id'] = $v['stc_id'];
                $maps['goods_state'] = 1;
                $maps['goods_sale'] = 1;
                $store_goods_class[$k]['goods'] = $goods->field('goods_id,goods_title,goods_thumb,goods_price,goods_markprice')->where($maps)->select();
            }
            $this->assign('store_goods_class',$store_goods_class);
        }

        $this->display();
    }


    //店铺商品分类页
    public function category(){

        //类别id
        if(I('get.stc_id')){
            $where['stc_id'] = I('stc_id'); //类别id
            $this->assign('stc_id',I('stc_id'));
        }

        $where['store_id'] = I('store_id');//店铺id
        $where['goods_sale'] = 1;
        $where['goods_state'] = 1;
        if(I('get.created')){
            switch(I('created')){
                case 1 : $d = "goods_created desc";break;
                case 2 : $d = "goods_created asc";break;
            }
            $this->assign('created',I('created'));
        }
        if(I('shop_price')){

            switch(I('shop_price')){
                case 1 : $d = "goods_price desc";break;
                case 2 : $d = "goods_price asc";break;
            }
            $this->assign('shop_price',I('shop_price'));
        }


        $goods = M('Goods')->where($where)->order($d)->select();
//        p($goods);die;
        $this->assign('goods',$goods);
        $this->display();
    }
}